{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                {if condition="$Think.session.admin.levelNum ==50 or  $Think.session.admin.departid==2"}
                    <div class="layui-inline">
                        <button id="btnAdd" class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="add">添加关键词</button>
                    </div>
                {/if}
                <div class="layui-inline">
                    <input class="layui-input" name="keywords" id="keywords" autocomplete="off">
                </div>

                <button class="layui-btn" data-type="reload">搜索</button>
                累计消费：{$sum}  本月消费：  {$sumMonth}
            </div>


        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList" ></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    {if condition="$Think.session.admin.levelNum ==50 or  $Think.session.admin.departid==2"}
                    <button class="layui-btn layui-btn-sm" lay-event="set_allUp">批量启用</button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="set_allStop">批量暂停</button>

<!--                    <button class="layui-btn layui-btn-sm" lay-event="allDel">批量删除</button>-->
<!--                    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>-->
                    {/if}

                </div>
            </script>
            <script type="text/html" id="state">
                <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="正常|停用" lay-filter="state" {{ d.state== 1 ? 'checked' : '' }} disabled>
                <!--                {if condition="$Think.session.admin.levelNum <10 "}disabled{/if}-->
            </script>
            <script type="text/html" id="typeid">
                {{#  if(d.typeid ==1){ }}百度{{#  } }}
                {{#  if(d.typeid ==2){ }}360{{#  } }}
                {{#  if(d.typeid ==3){ }}百度移动端{{#  } }}
                {{#  if(d.typeid ==4){ }}sogou{{#  } }}
                {{#  if(d.typeid ==5){ }}神马{{#  } }}
            </script>
            <script type="text/html" id="istop">
                {{#  if(d.istop ==1){ }}
                    {{d.topMoney}}
                {{#  } }}
            </script>
            <script type="text/html" id="op">
                {{#  if(d.state==1){ }}
                    {if condition="$Think.session.admin.levelNum ==50 or  $Think.session.admin.departid==2"}
                    <a class="layui-btn layui-btn-xs " lay-event="edit" >编辑</a>
                    {/if}

                    <a class="layui-btn layui-btn-xs" lay-tips="‘{{d.name}}’关键词明细" lay-text="‘{{d.name}}’关键词明细" lay-href="{:url(('/admin/money/keylist'))}?keyID={{d.id}}">明细</a>

                {if condition="$Think.session.admin.levelNum ==50 or ($Think.session.admin.levelNum>1 and $Think.session.admin.departid==2)"}
                    <a class="layui-btn layui-btn-xs" lay-event="keyKf">扣费</a>
                {/if}



<!--                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>-->
                {{#  } }}

            </script>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util','form'], function () {
        var wid={$webID};
        //表格
        var $ = layui.$,form = layui.form,layer = layui.layer,table = layui.table;
        table.render({
            elem: '#tbList',
            url: "{:url('getList')}",
            where: {webID:wid},
            toolbar: '#toolbarDemo',
            cellMinWidth: 80,
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', fixed: 'left', title: 'ID',width:66},
                {field: 'typeid',fixed: 'left',title: '平台', templet:'#typeid'},
                {field: 'name',fixed: 'left',title: '关键词',sort:true},
                {field: 'money_one',  title: '首页价格'},
                {field: 'money_two',  title: '二页价格'},
                {field: 'moneypay', title: '累计消费',sort:true},
                {field: 'nowPm', title: '当前排名',sort:true},
                {field: 'zPm', title: '昨日排名',sort:true},
                {field: 'state', title: '状态',width:96, templet: '#state'},
                // {field: 'intime',  title: '录入时间',width:120,sort:true},
                {fixed: 'right', title: '操作',width:200, toolbar: '#op'}
            ]],
            page: true,
            limit:15
        });


        //头工具栏事件
        table.on('toolbar(tbList)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            var idArr1='0';  //需要操作的id

            switch(obj.event){
                case 'set_allUp': //批量启用
                    for(var i=0;i<data.length;i++){
                        //状态为停用的才计算数量
                        if(data[i]['state']==3){
                            idArr1 =idArr1+','+data[i]['id'];
                        }
                    }
                    idArr1=idArr1.replace("0,","");
                    layer.open({
                        content: '确认批量启用?'
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            console.log(idArr1);
                            $.post("{:url('set_allUp')}",{webID:wid,ids1:idArr1}, function (result) {
                                if (result['code'] == 0) {
                                    location.reload();
                                } else {
                                    layer.msg(result['msg'], {icon: 3, time: 3000});
                                }
                            });
                            form.render();
                            layer.close(index);
                        }
                        ,btn2: function(index, layero){
                            layer.close(index);
                        }
                    });

                    break;
                case 'set_allStop':  //批量暂停
                    for(var i=0;i<data.length;i++){
                        //状态为正常的才计算数量
                        if(data[i]['state']==1){
                            idArr1 =idArr1+','+data[i]['id'];
                        }
                    }
                    console.log(idArr1);
                    layer.open({
                        content: '确认批量暂停?'
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            $.post("{:url('set_allStop')}",{webID:wid,ids1:idArr1}, function (result) {
                                if (result['code'] == 0) {
                                    location.reload();
                                } else {
                                    layer.msg(result['msg'], {icon: 3, time: 3000});
                                }
                            });
                            form.render();
                            layer.close(index);
                        }
                        ,btn2: function(index, layero){
                            layer.close(index);
                        }
                    });

                    break;
                case 'allDel':
                    for(var i=0;i<data.length;i++){
                        if(data[i]['state']==1){
                            idArr1 =idArr1+','+data[i]['id'];
                        }
                    }
                    layer.open({
                        content: '确认批量删除?'
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            $.post("{:url('all_del')}",{webID:wid,ids1:idArr1}, function (result) {

                                if (result['code'] == 0) {
                                    location.reload();
                                } else {
                                    layer.msg(result['msg'], {icon: 3, time: 3000});
                                }
                            });
                            form.render();
                            layer.close(index);
                        }
                        ,btn2: function(index, layero){
                            layer.close(index);
                        }
                    });

                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });


        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除', {icon: 3, title: '提示'}, function (index) {
                    // obj.del();
                    // layer.close(index);
                    $.post("{:url('del')}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '修改内容',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }else if (obj.event === 'keyKf') {
                var url = "{:url('/admin/money/keykf')}?keyID=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '关键词扣费',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }
        });

        //监听推荐
        form.on('switch(state)', function (data) {
            //停用
            var id=data.value;
            layer.open({
                content: '确认修改该关键词状态?'
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    $.post("{:url('set_state')}",{id:id,'state': data.elem.checked ? 1 : 3}, function (result) {
                        if (result['code'] == 0) {
                            location.reload();
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                    form.render();
                    layer.close(index);
                }
                ,btn2: function(index, layero){
                    layer.close(index);
                }
            });
            return false;



        });


        //操作
        var $ = layui.$, active = {
            add: function (obj) {
                var index = layer.open({
                    type: 2,
                    title: '添加站点',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}?webID="+wid,
                    zIndex: layer.zIndex
                });
                // layer.full(index);
            },
            reload: function(){
                var order=$('#order').val();
                var pid=$('#pid').val();
                var cid=$('#cid').val();
                var keywords=$('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        pid: pid
                        ,cid: cid
                        ,order: order
                        ,keywords: keywords
                    }
                });

            }
        };

        // $("#btnAdd").on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        // });

        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}